package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomerBean;
import com.project.mapper.ICustomerMapper;
import com.project.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private ICustomerMapper customerMapper;

    @Override
    public IPage<CustomerBean> cutByItem(Integer pageNO, String name, String type) {
        QueryWrapper<CustomerBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("c_name", name);
        }
        if (type != null && type.length() != 0) {
            qw.like("c_type", type);
        }

        return customerMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public CustomerBean findById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    public void update(Integer id, String person, String phone, String type) {
       CustomerBean customerBean=  customerMapper.selectById(id);
       customerBean.setPerson(person);
       customerBean.setPhone(phone);
       customerBean.setType(type);
       customerMapper.updateById(customerBean);

    }

    @Override
    public void add(CustomerBean customerBean) {

        customerMapper.insert(customerBean);

    }

    @Override
    public List<CustomerBean> findAll() {

        return customerMapper.selectList(null);
    }
}
